package com.sgcc.sx.pmos.e_sx_home.fragment;

import android.graphics.Color;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.sgcc.pmos.nativesdk.R;
import com.sgcc.sx.pmos.arouter.home.HomeArouterPath;
import com.sgcc.sx.pmos.common.base.MvpBaseRecycleFragment;
import com.sgcc.sx.pmos.e_sx_home.adapter.SellElectricityCompanyAdapter;
import com.sgcc.sx.pmos.e_sx_home.bean.MarketBean;
import com.sgcc.sx.pmos.e_sx_home.contract.SellElectricityCompanyContract;
import com.sgcc.sx.pmos.e_sx_home.presenter.SellElectricityCompanyPresenter;

import java.util.List;

public class SellElectricityCompanySearchFragment extends MvpBaseRecycleFragment< MarketBean.DataDTO.ListDTO, SellElectricityCompanyPresenter> implements SellElectricityCompanyContract.View {

    private SellElectricityCompanyAdapter mSellElectricityCompanyAdapter;
    private String num;
    private String ele;
    private String credit;
    private RelativeLayout mRlDeft;
    private TextView mTvDeftSort;
    private ImageView iv_credit_sort_up;
    private ImageView iv_credit_sort_down;

    private RelativeLayout mRlNum;
    private TextView mTvNumSort;
    private ImageView mIvDeftSortUp;
    private ImageView mIvDeftSortDown;
    private RelativeLayout mRlEle;
    private TextView mTvEleSort;
    private ImageView mIvEleSortUp;
    private ImageView mIvEleSortDown;
    private String searchContent;

    @Override
    protected SellElectricityCompanyPresenter createPresenter() {
        return new SellElectricityCompanyPresenter();
    }

    @Override
    public View getHeaderLayout(RelativeLayout parent) {
        View head_view = getLayoutInflater().inflate(R.layout.header_sellelectricitycompany_layout, parent, false);
        findViewByIds(head_view);
        return head_view;
    }

    private void findViewByIds(View head_view) {
        mRlDeft = (RelativeLayout) head_view.findViewById(R.id.rl_deft);
        iv_credit_sort_up=head_view.findViewById(R.id.iv_credit_sort_up);
        iv_credit_sort_down=head_view.findViewById(R.id.iv_credit_sort_down);

        mTvDeftSort = (TextView) head_view.findViewById(R.id.tv_deft_sort);
        mRlNum = (RelativeLayout) head_view.findViewById(R.id.rl_num);
        mTvNumSort = (TextView) head_view.findViewById(R.id.tv_num_sort);
        mIvDeftSortUp = (ImageView) head_view.findViewById(R.id.iv_deft_sort_up);
        mIvDeftSortDown = (ImageView) head_view.findViewById(R.id.iv_deft_sort_down);
        mRlEle = (RelativeLayout) head_view.findViewById(R.id.rl_ele);
        mTvEleSort = (TextView) head_view.findViewById(R.id.tv_ele_sort);
        mIvEleSortUp = (ImageView) head_view.findViewById(R.id.iv_ele_sort_up);
        mIvEleSortDown = (ImageView) head_view.findViewById(R.id.iv_ele_sort_down);

        mRlDeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mTvDeftSort.setTextColor(Color.parseColor("#0092FF"));
                mTvNumSort.setTextColor(Color.parseColor("#999999"));
                mTvEleSort.setTextColor(Color.parseColor("#999999"));
                ele = "";
                num = "";
                if (TextUtils.isEmpty(credit)) {
                    credit = "1";
                    iv_credit_sort_up.setBackgroundResource(R.mipmap.home_top_store_b_up_pre);
                    iv_credit_sort_down.setBackgroundResource(R.mipmap.home_top_store_b);
                    mIvDeftSortUp.setBackgroundResource(R.mipmap.home_top_store_b_up);
                    mIvDeftSortDown.setBackgroundResource(R.mipmap.home_top_store_b);
                    mIvEleSortUp.setBackgroundResource(R.mipmap.home_top_store_b_up);
                    mIvEleSortDown.setBackgroundResource(R.mipmap.home_top_store_b);
                } else if ("1".equals(credit)) {
                    credit = "0";
                    iv_credit_sort_up.setBackgroundResource(R.mipmap.home_top_store_b_up);
                    iv_credit_sort_down.setBackgroundResource(R.mipmap.home_top_store_b_pre);

                    mIvDeftSortUp.setBackgroundResource(R.mipmap.home_top_store_b_up);
                    mIvDeftSortDown.setBackgroundResource(R.mipmap.home_top_store_b);
                    mIvEleSortUp.setBackgroundResource(R.mipmap.home_top_store_b_up);
                    mIvEleSortDown.setBackgroundResource(R.mipmap.home_top_store_b);
                } else {
                    credit = "";
                    iv_credit_sort_up.setBackgroundResource(R.mipmap.home_top_store_b_up);
                    iv_credit_sort_down.setBackgroundResource(R.mipmap.home_top_store_b);
                    mIvDeftSortUp.setBackgroundResource(R.mipmap.home_top_store_b_up);
                    mIvDeftSortDown.setBackgroundResource(R.mipmap.home_top_store_b);
                    mIvEleSortUp.setBackgroundResource(R.mipmap.home_top_store_b_up);
                    mIvEleSortDown.setBackgroundResource(R.mipmap.home_top_store_b);
                    mTvDeftSort.setTextColor(Color.parseColor("#0092FF"));
                    mTvNumSort.setTextColor(Color.parseColor("#999999"));
                    mTvEleSort.setTextColor(Color.parseColor("#999999"));
                }
                refresh(false);
            }
        });

        mRlNum.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mTvDeftSort.setTextColor(Color.parseColor("#999999"));
                mTvNumSort.setTextColor(Color.parseColor("#0092FF"));
                mTvEleSort.setTextColor(Color.parseColor("#999999"));
                ele = "";
                credit = "";
                if (TextUtils.isEmpty(num)) {
                    num = "1";
                    iv_credit_sort_up.setBackgroundResource(R.mipmap.home_top_store_b_up);
                    iv_credit_sort_down.setBackgroundResource(R.mipmap.home_top_store_b);
                    mIvDeftSortUp.setBackgroundResource(R.mipmap.home_top_store_b_up_pre);
                    mIvDeftSortDown.setBackgroundResource(R.mipmap.home_top_store_b);
                    mIvEleSortUp.setBackgroundResource(R.mipmap.home_top_store_b_up);
                    mIvEleSortDown.setBackgroundResource(R.mipmap.home_top_store_b);
                } else if ("1".equals(num)) {
                    num = "0";
                    iv_credit_sort_up.setBackgroundResource(R.mipmap.home_top_store_b_up);
                    iv_credit_sort_down.setBackgroundResource(R.mipmap.home_top_store_b);
                    mIvDeftSortUp.setBackgroundResource(R.mipmap.home_top_store_b_up);
                    mIvDeftSortDown.setBackgroundResource(R.mipmap.home_top_store_b_pre);
                    mIvEleSortUp.setBackgroundResource(R.mipmap.home_top_store_b_up);
                    mIvEleSortDown.setBackgroundResource(R.mipmap.home_top_store_b);
                } else {
                    num = "";
                    iv_credit_sort_up.setBackgroundResource(R.mipmap.home_top_store_b_up);
                    iv_credit_sort_down.setBackgroundResource(R.mipmap.home_top_store_b);
                    mIvDeftSortUp.setBackgroundResource(R.mipmap.home_top_store_b_up);
                    mIvDeftSortDown.setBackgroundResource(R.mipmap.home_top_store_b);
                    mIvEleSortUp.setBackgroundResource(R.mipmap.home_top_store_b_up);
                    mIvEleSortDown.setBackgroundResource(R.mipmap.home_top_store_b);
                    mTvDeftSort.setTextColor(Color.parseColor("#0092FF"));
                    mTvNumSort.setTextColor(Color.parseColor("#999999"));
                    mTvEleSort.setTextColor(Color.parseColor("#999999"));
                }
                refresh(false);
            }
        });
        mRlEle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mTvDeftSort.setTextColor(Color.parseColor("#999999"));
                mTvNumSort.setTextColor(Color.parseColor("#999999"));
                mTvEleSort.setTextColor(Color.parseColor("#0092FF"));
                num = "";
                credit = "";
                if (TextUtils.isEmpty(ele)) {
                    ele = "1";
                    iv_credit_sort_up.setBackgroundResource(R.mipmap.home_top_store_b_up);
                    iv_credit_sort_down.setBackgroundResource(R.mipmap.home_top_store_b);
                    mIvDeftSortUp.setBackgroundResource(R.mipmap.home_top_store_b_up);
                    mIvDeftSortDown.setBackgroundResource(R.mipmap.home_top_store_b);
                    mIvEleSortUp.setBackgroundResource(R.mipmap.home_top_store_b_up_pre);
                    mIvEleSortDown.setBackgroundResource(R.mipmap.home_top_store_b);
                } else if ("1".equals(ele)) {
                    ele = "0";
                    iv_credit_sort_up.setBackgroundResource(R.mipmap.home_top_store_b_up);
                    iv_credit_sort_down.setBackgroundResource(R.mipmap.home_top_store_b);
                    mIvDeftSortUp.setBackgroundResource(R.mipmap.home_top_store_b_up);
                    mIvDeftSortDown.setBackgroundResource(R.mipmap.home_top_store_b);
                    mIvEleSortUp.setBackgroundResource(R.mipmap.home_top_store_b_up);
                    mIvEleSortDown.setBackgroundResource(R.mipmap.home_top_store_b_pre);
                } else {
                    ele = "";
                    iv_credit_sort_up.setBackgroundResource(R.mipmap.home_top_store_b_up);
                    iv_credit_sort_down.setBackgroundResource(R.mipmap.home_top_store_b);
                    mIvDeftSortUp.setBackgroundResource(R.mipmap.home_top_store_b_up);
                    mIvDeftSortDown.setBackgroundResource(R.mipmap.home_top_store_b);
                    mIvEleSortUp.setBackgroundResource(R.mipmap.home_top_store_b_up);
                    mIvEleSortDown.setBackgroundResource(R.mipmap.home_top_store_b);
                    mTvDeftSort.setTextColor(Color.parseColor("#0092FF"));
                    mTvNumSort.setTextColor(Color.parseColor("#999999"));
                    mTvEleSort.setTextColor(Color.parseColor("#999999"));
                }
                refresh(false);
            }
        });
    }

    @Override
    public BaseQuickAdapter getAdapter() {
        mSellElectricityCompanyAdapter = new SellElectricityCompanyAdapter(mContext);
        mSellElectricityCompanyAdapter.setOnItemClickListener(new SellElectricityCompanyAdapter.OnItemClickListener() {
            @Override
            public void itemClickListener(int position,  MarketBean.DataDTO.ListDTO bean) {
                ARouter.getInstance()
                        .build(HomeArouterPath.HOME_MEMBERSMARKETINFO)
                        .withString("membersId", bean.getMemberSdId())
                        .navigation();
            }
        });
        return mSellElectricityCompanyAdapter;
    }

    @Override
    public void getDataRequest(int listSize, boolean isloading) {
        super.getDataRequest(listSize, isloading);
        mPresenter.getSellElectricityCompanyList(searchContent,credit,num, ele, listSize, isloading);
    }

    @Override
    public void querySuccess(boolean isHasNextPage, List<MarketBean.DataDTO.ListDTO> list) {
        loadSuccess(isHasNextPage, list);
    }
    @Override
    public void totalQuerySuccess(int total) {
    }

    public void setSearchContent(String searchContent) {
        this.searchContent = searchContent;
        refresh(true);
    }
}
